package br.upe.dsc.caeto.ui;


import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import org.jdesktop.application.Application;

import br.upe.dsc.caeto.core.hierarchy.Hierarchy;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonEditor extends AbstractCellEditor implements
		TableCellEditor, ActionListener {
	protected static final String EDIT = "edit";
	private static final int HIERARCHY_1 = 1;
	private static final int HIERARCHY_2 = 2;
	private static final int HIERARCHY_3 = 3;
	private static final int HIERARCHY_4 = 4;

	private JButton button;
	private JTable table;
	private int hierarchyNumber;
	private Hierarchy hierarchy;
	private javax.swing.JFrame parent;

	public ButtonEditor(javax.swing.JFrame parent, JTable table,
			Hierarchy hierarchy) {
		button = new JButton();
		this.table = table;
		this.parent = parent;
		this.hierarchyNumber = 1;
		this.hierarchy = hierarchy;

		button.setBorderPainted(true);
		button.setText("Configure");
		button.setActionCommand(EDIT);
		button.addActionListener(this);
	}

	public void setHierarchy(int hierarchy) {
		this.hierarchyNumber = hierarchy;
	}

	public void actionPerformed(ActionEvent e) {
		if (EDIT.equals(e.getActionCommand())) {
			
			/*
			 * The user has clicked the cell, so
			 * bring up the dialog.
			 */
			if (table.getSelectedRow() == table.getRowCount()-1) {
				TechnologyParametersDialog tecno = new
					TechnologyParametersDialog();
				tecno.setVisible(true);
			}
			else {
				Application.getInstance(CaetoApplication.class)
				.show(new CacheParametersDialog(
						parent, getCacheName(), hierarchy));
			}

			/* Make the renderer reappear */
			fireEditingStopped();
			table.getModel().setValueAt("...", table.getSelectedRow(), 1);
		}
	}

	private String getCacheName() {
		if (hierarchyNumber == HIERARCHY_1) {
			switch(table.getSelectedRow()) {
			case 0: return "dl1";
			case 1: return "il1";
			case 2: return "dl2";
			case 3: return "il2";
			}
		}
		else if (hierarchyNumber == HIERARCHY_2) {
			switch(table.getSelectedRow()) {
			case 0: return "dl1";
			case 1: return "il1";
			case 2: return "L2";
			}
		}
		else if (hierarchyNumber == HIERARCHY_3) {
			switch(table.getSelectedRow()) {
			case 0: return "L1";
			case 1: return "L2";
			}
		}
		else {
			switch(table.getSelectedRow()) {
			case 0: return "dl1";
			case 1: return "il1";
			}
		}
		return null; //Error!
	}

	//Implement the one CellEditor method that AbstractCellEditor doesn't.
	public Object getCellEditorValue() {
		return "";
	}

	//Implement the one method defined by TableCellEditor.
	public Component getTableCellEditorComponent(JTable table,
			Object value,
			boolean isSelected,
			int row,
			int column) {
		return button;
	}
}
